package org.dandelion.papercheck.service.impl;

import org.dandelion.papercheck.dao.UserDao;
import org.dandelion.papercheck.entity.Article;
import org.dandelion.papercheck.entity.History;
import org.dandelion.papercheck.entity.User;
import org.dandelion.papercheck.service.UserService;
import org.dandelion.papercheck.util.JWTUtils;
import org.dandelion.papercheck.util.MD5Util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.*;

@Service
public class UserServiceImp implements UserService {
    private static final Logger logger = LoggerFactory.getLogger(UserService.class);
    private final UserDao userDao;

    @Autowired
    public UserServiceImp(UserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public User login(String username, String password) {
        List<User> userList = userDao.findByUsername(username);

        if (userList.size() == 1) {
            User user = userList.get(0);

            String s = MD5Util.encryptAddSalt(password, String.valueOf(user.getRegisterTime().getTime()));

            if (user.getPassword().equals(s)) {
                return user;
            } else {
                return null;
            }
        } else {
            return null;
        }
    }

    @Override
    public String generateJWT(User user) {
        var map = new HashMap<String, String>();
        map.put("id", user.getId().toString());
        map.put("name", user.getUsername());
        map.put("vip", user.getIsVip().toString());
        map.put("admin", user.getIsAdmin().toString());
        return JWTUtils.getToken(map);
    }

    @Override
    public Boolean checkUsernameDup(String username) {
        List<User> list = userDao.findByUsername(username);
        return list.size() > 0;
    }

    @Override
    public User register(String username, String password, String phone, String email) {
        User user = new User();

        Timestamp time = Timestamp.valueOf(LocalDateTime.now());
        user.setRegisterTime(time);
        user.setUsername(username);

        String pwd = MD5Util.encryptAddSalt(password, String.valueOf(time.getTime()));

        user.setPassword(pwd);
        user.setPhone(phone);
        user.setEmail(email);
        user.setIsVip(false);
        user.setIsAdmin(false);

        return userDao.save(user);
    }

    @Override
    public User resetPwd(User user, String password) {
        user.setPassword(password);
        return userDao.save(user);
    }

    @Override
    public List<User> getUserList(Integer size, Integer page) {
        Pageable pageable;
        pageable = PageRequest.of(page, size, Sort.Direction.DESC, "username");
        return userDao.findByIsAdmin(false, pageable);
    }

    @Override
    public Boolean changeIsVip(Long userId, Boolean isVip) {
        Optional<User> user = userDao.findById(userId);
        logger.info("Chang vip----");
        logger.info(userId + "");

        if (user.isPresent()) {
            User user1 = user.get();
            user1.setIsVip(isVip);
            return userDao.save(user1) != null;
        } else {
            return false;
        }
    }
}
